#ifndef LINE_H
#define LINE_H

#include <QGraphicsObject>
#include <QThread>
#include <QList>
#include <QColor>
#include <QRect>
#include <QPoint>
#include <QLabel>
#include <QPainter>
#include <QLine>
#include "exparrival.h"


/****

The Line Class.
A Line Object is an individual graphics object and is controlled by a QThread.
It has the following properties:

    Each line has a random number of segments; for each segments,
    there will be a turning with 45 degree; the whole line should have the
    same colour scheme.
    Each line is an individual QThread, the drawing is started by the Line::start()
    method. There will be several stages for a line life time:
        Draw stage: draw the line from the current pointer position.
            This stage is terminated when the cur_point is about to hit the boundary.
            Each upgrade will either extend the line by a number of pixels (speed), or make a
            turning when the turn timer runs out (draw_time).
            Turning timer is set when: a. the thread is started; b. line is made a turn
            The timer is set randomly within a scope [mintime, maxtime].
        Show stage: when the line finished drawing, the line will be displayed with full
            opacity for a while.
            The show time is controlled by the show_timer. When show_timer runs out, this
            stage is then occupied by fade stage.
        Fade stage: the line thread is about to die. Before the end of life time, the line is set
            with a fade_timer; the opacity of the whole line will also be controlled by the fade_timer.
            If fade_timer runs out, this line thread will be terminated and wiped out.

****/

#define HRT       0.7071067811865475
#define DEF_FADE  50
#define DEF_SHOW  250
#define DEF_TRATE 0.025
#define DEF_TMIN  5

class Line : public QGraphicsObject, public QThread
{
    Q_OBJECT
public:
    enum Direction { N = 0, NE, E, SE, S, SW, W, NW };
    enum LineStage { Draw, Show, Fade, Dead };
    Line( int w, int h, int spd, QObject *parent = 0 );
    QRectF boundingRect () const
    {
        return QRectF( -width/2, -height/2, width, height );
    }
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
protected:
    void run();
    void mousePressEvent ( QGraphicsSceneMouseEvent * event );
private:
    void make_a_turn();  // make a random  +/- 45 degree turn
    void make_a_start(); // make a starting point for the line
    void draw_a_step();  // extend the line towards the current direction
    void show_a_step();
    void fade_a_step();
    int life_timer;  // how much time has passed since start
    int finish_time; // how long it takes to finish drawing
    int show_timer;  // how long to show the finished line
    int fade_timer;  // how long to fade the finished line
    int turn_timer;  // how much time left to make a turning

    LineStage cur_stage;

    Direction cur_direct;  // current marching direction
    QPoint    cur_pointer; // current head pointer of the line

    int speed;        // how many pixels to draw each upgrade
    QColor color;
    int width, height;

    QVector <QLine> lines;
};

#endif // LINE_H
